package edu.mit.media.fv.aqview
{
	import edu.mit.media.fv.blocks.*;
	import edu.mit.media.fv.components.*;
	import edu.mit.media.fv.events.*;
	import edu.mit.media.fv.fluidspace.*;
	import edu.mit.media.fv.aqwidgets.*;
	
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.text.*;
	
	public class AQPollBuilder extends AQViewer
	{
		var _sendButton:DisplayObject;
		var _switchButton:DisplayObject;
		var _addButton:DisplayObject;
		var _deleteButton:DisplayObject;
		var _pollType:AQComboBox;
		var _duration:AQComboBox;
		var _tags:AQTextField;
		var _question:AQPollQuestion;
		
		var _descripters:Array = new Array();
		
		private var isViewingDescription:Boolean = true;
		private var choices:Array;//array of TextField
		private var viewport:Sprite;
		private var scroll:FVGlassScrollPane;
		private var optionInFocus:AQPollOption;
		
		public function AQPollBuilder()
		{
			super();
			this.scroll = new FVGlassScrollPane(10,100,600,280);
			this.scroll.background = true;
			FluidEventHandler.addEventListener(WidgetEvent.DISPLAY_POLL_BUILDER, displayWidget);
		}
		public function displayWidget(event:WidgetEvent = null):void{
			FluidEventHandler.dispatchEvent(new WidgetEvent(WidgetEvent.DISPLAY_WIDGET, this, "Poll"));

			this._tags.text = "";
            this._question.init(-1, '', true);
			this._duration.setItems(["1 Hour", "2 Hour", "3 Hour"]);
			this._pollType.setItems(["Radio Box","Check Box","Ranking"]);
			this._duration.selectedIndex = 1;
			this._pollType.selectedIndex = 1;
			
			this.choices = new Array();
			this.viewport = new Sprite();
			this.scroll.viewport = this.viewport;
			this.addOption();
            
            this.swapChildren(this._pollType, this._duration);
            
		}
		public function switchView(event:MouseEvent):void{
			var i:int = 0;
			if(isViewingDescription){
				//this.removeChild(this._pollBuilder1);
				for(i = 0; i<this._descripters.length; i++){
					this.removeChild(DisplayObject(this._descripters[i]));
				}
				this.addChild(this._addButton);
				this.addChild(this._deleteButton);
				this.addChild(this.scroll);
			}else{
				this.removeChild(this._addButton);
				this.removeChild(this._deleteButton);
				this.removeChild(this.scroll);
				for(i = 0; i<this._descripters.length; i++){
					this.addChild(DisplayObject(this._descripters[i]));
				}
				//this.addChild(this._pollBuilder1);
			}
			isViewingDescription = !isViewingDescription;
		}
		private function sendMessage(event:MouseEvent = null):void{
			/*
            var choicesString:Array = new Array();
			for (var i:int = 0; i<this.choices.length; i++){
				choicesString.push(AQPollOption(this.choices[i]).text);
			}
            */
			var choiceReprs:Array = new Array();
            for (var i:int=0; i<this.choices.length; i++)
            {
                choiceReprs.push(this.choices[i].getRepresentation());
            }
			var data:Object = new Object();
			data.command = FluidConstants.REQUEST_SEND_OBJECT;
			data.type = FluidConstants.POLL;
			data.priority = 1;
			data.tags = this._tags.text;
			data.question = this._question.text;
            data.questionID = this._question.id;
            data.poll_type = this._pollType.selectedIndex;
			data.duration = this._duration.selectedIndex+1;
			data.choices = choiceReprs;
			data.id = -1;
			var e:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(e);
			
			FluidEventHandler.dispatchEvent(new WidgetEvent(WidgetEvent.DISPLAY_WORKSPACE));
		}
		private function addOption(event:MouseEvent = null):void{
			var option:AQPollOption = new AQPollOption(-1,'',true);
			option.addEventListener(AQPollOptionEvent.POLL_SELECTED, processPollSelected);
			this.choices.push(option);
			this.viewport.addChild(option);
			this.reformVieport();
			this.scroll.reform();
		}
		private function processPollSelected(event:AQPollOptionEvent):void{
			if(this.optionInFocus != null && this.optionInFocus != event.pollOption){
					this.optionInFocus.focusOption(false);
				}
				this.optionInFocus = event.pollOption;
				this.optionInFocus.focusOption(true);
		}
		private function removeOption(event:MouseEvent):void{
			trace("enter removal");
			if(this.optionInFocus != null){
				trace(this.optionInFocus);
				var index:int = this.choices.indexOf(this.optionInFocus);
				if(index != -1){
					this.choices.splice(index, 1);				
				}
				this.viewport.removeChild(this.optionInFocus);
				this.reformVieport();
			}
		}
		private function reformVieport():void{
			var maxY:int = 0;
			for(var i:int = 0; i<this.viewport.numChildren; i++){
				var c:DisplayObject = this.viewport.getChildAt(i);
				c.y = maxY;
				maxY =maxY+c.height;
			}
		}

	}
}